var Sql = {
    removeRemark: function(d) {
        var c = /^\/{2,}/,
        b = /^-{2,}/,
        a = /^\/\*/,
        e = /("([^\\\"]*(\\.)?)*")|('([^\\\']*(\\.)?)*')|(\/{2,}.*?(\r|\n|$))|(\-{2,}.*?(\r|\n|$))|(\/\*(\n|.)*?\*\/)/g;
        return d.replace(e,
        function(f) {
            return (c.test(f) || b.test(f) || a.test(f)) ? "": f
        })
    },
    autoComplete: function(a) {
        a.allUserHintItems = {};
        a.xsyncLoadItems = Sql.xsyncLoadItems
    },
    xsyncLoadItems: function(c, e) {
        var b = Sql.extractTableNames(c.getValue(), c.getCursor()),
        a = e.toLowerCase(),
        d;
        Wb.each(b,
        function(f) {
            if (f.prefix.toLowerCase() == a) {
                d = f;
                f.prefix = e;
                return false
            }
        });
        if (!d) {
            d = {
                prefix: e,
                table: null
            }
        }
        Wb.request({
            url: "m?xwl=dev/datainfo/SQL/dbm/get-all-fields",
            async: false,
            params: {
                object: d,
                jndi: c.jndi
            },
            showMask: false,
            showError: false,
            callback: function() {
                c.isScanning = false
            },
            success: function(g) {
                var f = Wb.decode(g.responseText);
                if (!f) {
                    f = {}
                }
                c.allUserHintItems = f
            }
        })
    },
    replaceAll: function(b, c, a) {
        return b.replace(new RegExp("\\b" + c + "\\b", "gi"), a)
    },
    getCurrentSql: function(e, d) {
        var c, a, b = "@@$!~";
        e = e.split("\n");
        c = e[d.line];
        e[d.line] = c.substring(0, d.ch) + b + c.substring(d.ch);
        e = e.join("\n");
        e = Sql.splitSql(e);
        Wb.each(e,
        function(f) {
            if (f.indexOf(b) != -1) {
                a = Wb.replace(f, b, "");
                return false
            }
        });
        return Sql.removeRemark(a)
    },
    extractTableNames: function(i, f) {
        var e, a, c, d, h, b = [],
        g = ["where", "order", "group", "by", "as", "all", "and", "from", "on", "or", "not", "distinct", "select", "update", "delete", "insert", "truncate", "drop", "index", "alter", "left", "outer", "inner", "full", "join", "corss", "using", "desc", "asc", "between", "union", "intersect", "except", "is", "null", "having"];
        i = Sql.getCurrentSql(i, f);
        i = Wb.replaceAll(i, ";", " ");
        i = Wb.replaceAll(i, "\n", " ");
        i = Wb.replaceAll(i, "\t", " ");
        h = i.toLowerCase();
        d = h.search(/\bfrom\b/);
        if (d != -1) {
            i = i.substring(d + 5);
            h = h.substring(d + 5)
        }
        Wb.each(g,
        function(j) {
            i = Sql.replaceAll(i, j, ",")
        });
        c = i.split(",");
        Wb.each(c,
        function(j) {
            j = j.trim();
            if (!j) {
                return
            }
            d = j.indexOf(" ");
            e = j.substring(0, d).trim();
            a = j.substring(d).trim();
            if (Sql.verifyName(e, true) === true && Sql.verifyName(a) === true) {
                b.push({
                    table: e,
                    prefix: a
                })
            }
        });
        return b
    },
    verifyName: function(d, a) {
        var f, e, b = d.length;
        for (e = 0; e < b; e++) {
            f = d.charAt(e);
            if (! (f >= "a" && f <= "z" || a && f == "." || f >= "A" && f <= "Z" || f == "_" || e > 0 && (f >= "0" && f <= "9"))) {
                return Wb.format(Str.invalidChar, f)
            }
        }
        return true
    },
    splitSql: function(b) {
        var a = /(?!;|$)[^;']*(('[^']*')[^;']*)*/g;
        return b.match(a)
    }
};